//
//  OnlyOnePickerView.m
//  微他
//
//  Created by 微他 on 14-7-31.
//  Copyright (c) 2014年 微他. All rights reserved.
//

#import "OnlyOnePickerView.h"
#import "SRManager.h"

@interface OnlyOnePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIPickerView *_pickerView;
    NSMutableArray *_nameArr;
}
@end

@implementation OnlyOnePickerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didFindPeripheral)
                                                     name:@"didFindPeripheral"
                                                   object:nil];
        [self initInterface];
        [self initData];
    }
    return self;
}

- (void)initInterface
{
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 0)];
    _pickerView.backgroundColor = [UIColor clearColor];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.hidden = YES;
    [self addSubview:_pickerView];
    
}

- (void)initData
{
    NSArray *devices = [SRManager sharedInstance].findDevices;
    if (devices.count > 0) {
        _pickerView.hidden = NO;
        _nameArr = [[NSMutableArray alloc] init];
        [devices enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            CBPeripheral * per = obj;
            [_nameArr addObject:per.name];
        }];
        [_pickerView reloadAllComponents];
        if (_nameArr.count > 0) {
            [_pickerView selectRow:0 inComponent:0 animated:NO];
        }
        [self pickerView:_pickerView didSelectRow:0 inComponent:0];
    }
}

- (void)didFindPeripheral
{
    [self initData];
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] init];
    label.text = _nameArr[row];
    label.font = [UIFont boldSystemFontOfSize:17];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [SetFontColorProperty setFontColor:34 float2:34 float3:34];
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
    label.textColor = [UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1];
    self.selectPerName = label.text;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _nameArr.count;
}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    if (component == 0) {
//        return @"1";
//    }
//    return nil;
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
